package com.punai.costData.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.costData.bo.ZbStructItemChildBo;
import com.punai.costData.bo.ZbStructItemListBo;
import com.punai.costData.service.CostIndicatorService;
import com.punai.externalccb.domain.EcoCbbidnode;
import com.punai.externalccb.domain.EcoZbstructitem;
import com.punai.externalccb.mapper.EcoCbbidnodeMapper;
import com.punai.externalccb.mapper.EcoZbstructitemMapper;
import org.apache.commons.collections4.ListValuedMap;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CostIndicatorServiceImpl implements CostIndicatorService {

    @Autowired
    private EcoZbstructitemMapper zbstructitemMapper;
    @Autowired
    private EcoCbbidnodeMapper cbbidnodeMapper;


    @Override
    public List<ZbStructItemListBo> getUsedIndicator(String id, String stagetype) {
        List<EcoZbstructitem> ecoZbstructitems = zbstructitemMapper.selectList(
                Wrappers.<EcoZbstructitem>lambdaQuery()
                        .eq(EcoZbstructitem::getStagetype, stagetype)
                        .eq(EcoZbstructitem::getProId, id)
        );
        List<ZbStructItemListBo> kvbos = new ArrayList<>();
        Set<String> collect = ecoZbstructitems.stream().map(v -> v.getStructid() + "_" + v.getStructname()+"_"+(StrUtil.isBlank(v.getShowtype())?"11":v.getShowtype())).collect(Collectors.toSet());
        for (String s : collect) {
            ZbStructItemListBo kvbo = new ZbStructItemListBo();
            String[] split = s.split("_");
            kvbo.setId(split[0]);
            kvbo.setName(split[1]);
            kvbo.setShowType(split[2]);
            kvbos.add(kvbo);
        }
        return kvbos;
    }

    @Override
    public List<ZbStructItemChildBo> getIndicatorDetail(String id, String stagetype, String indicatorId) {

        List<EcoCbbidnode> cbbidnodes = cbbidnodeMapper.selectList(
                Wrappers.<EcoCbbidnode>lambdaQuery()
                        .eq(EcoCbbidnode::getStagetype, stagetype)
                        .eq(EcoCbbidnode::getNodetype, 2)
                        .eq(EcoCbbidnode::getProId, id)
        );
        List<ZbStructItemChildBo> res = new ArrayList<>();
        for (EcoCbbidnode cbBidNode : cbbidnodes) {
            ZbStructItemChildBo treeRoot = new ZbStructItemChildBo();
            treeRoot.setName(cbBidNode.getNodename());

            List<EcoZbstructitem> all = zbstructitemMapper.selectList(
                    Wrappers.<EcoZbstructitem>lambdaQuery()
                            .eq(EcoZbstructitem::getStagetype, stagetype)
                            .eq(EcoZbstructitem::getProId, id)
                            .eq(EcoZbstructitem::getStructid, indicatorId)
            );

            if(CollUtil.isEmpty(all)) {
                continue;
            }

            all.sort(Comparator.comparingInt(EcoZbstructitem::getKeyid));

            res.add(treeRoot);
            ListValuedMap<String, EcoZbstructitem> valuedHashMap = new ArrayListValuedHashMap<>();
            for (EcoZbstructitem zbStructItem : all) {
                if (StrUtil.equals(zbStructItem.getZbpid(), indicatorId)) {
                    zbStructItem.setZbpid("0");
                }
                valuedHashMap.put(zbStructItem.getZbpid(), zbStructItem);
            }

            List<EcoZbstructitem> roots = valuedHashMap.get("0");
            for (EcoZbstructitem root : roots) {
                ZbStructItemChildBo tree = new ZbStructItemChildBo();
                BeanUtil.copyProperties(root,tree);
                res.add(tree);
                this.getChildren(tree, valuedHashMap);
            }
        }
        return res;
    }

    private void getChildren(ZbStructItemChildBo p, ListValuedMap<String, EcoZbstructitem> valuedHashMap) {
        String zbid = p.getZbid();
        List<EcoZbstructitem> children = valuedHashMap.get(zbid);
        if (CollUtil.isNotEmpty(children)) {
            for (EcoZbstructitem child : children) {
                ZbStructItemChildBo tree = new ZbStructItemChildBo();
                BeanUtil.copyProperties(child,tree);
                p.getChildren().add(tree);
                this.getChildren(tree, valuedHashMap);
            }
        }
    }
}
